package com.foredu.fiqs.persist.hibernate;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.foredu.fiqs.domain.Salary;
import com.foredu.fiqs.persist.ISalaryDao;

public class HibernateSalaryDao extends HibernateAbstractDao<Salary, Integer>
		implements ISalaryDao {

	private static final long serialVersionUID = 1L;

	public HibernateSalaryDao() {
		super(Salary.class);
	}

	@SuppressWarnings({ "deprecation" })
	@Override
	public Salary findSalaryByUserIdAndDate(int userId, int month, int year) {
		Connection conn = this.getSession().connection();
		CallableStatement cstmt;
		Salary salary = new Salary();
		try {
			cstmt = conn.prepareCall("{call proc_querySalaryByIdAndDate(?,?,?)}");
			cstmt.setInt(1, userId);
			cstmt.setInt(2, month);
			cstmt.setInt(3, year);
			cstmt.execute();
			ResultSet rs = cstmt.getResultSet();
			if(rs.next()){
				salary.setId(rs.getInt(1));
				salary.setMonth(rs.getInt(2));
				salary.setPayDate(rs.getString(3));
				salary.setTotal(rs.getFloat(4));
				salary.setYear(rs.getInt(5));
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("查询出来的数据是:"+salary.getTotal());
		return salary;
	}

	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> findSalItemBySalaryId(int salaryId) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Connection conn = this.getSession().connection();
		CallableStatement cstmt;
		try {
			cstmt = conn.prepareCall("{call proc_querySalItemBySalaryId(?)}");
			cstmt.setInt(1, salaryId);
			cstmt.execute();
			ResultSet rs = cstmt.getResultSet();
			while(rs.next()){
				Map<String, Object> queryMap = new HashMap<String, Object>();
				queryMap.put("id", rs.getInt(1));
				queryMap.put("total", rs.getFloat(2));
				queryMap.put("name", rs.getString(6));
				list.add(queryMap);
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> findSalDetailBySalItemId(int salItemId) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Connection conn = this.getSession().connection();
		CallableStatement cstmt;
		try{
			cstmt = conn.prepareCall("{call proc_querySalDetailBySalItemId(?)}");
			cstmt.setInt(1, salItemId);
			cstmt.execute();
			ResultSet rs = cstmt.getResultSet();
			while(rs.next()){
				Map<String, Object> queryMap = new HashMap<String, Object>();
				queryMap.put("id", rs.getInt(1));
				queryMap.put("amount", rs.getFloat(2));
				queryMap.put("name", rs.getString(6));
				list.add(queryMap);
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}
